package org.yjc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.DatagridUtils;
import org.yjc.custom.JournalUtils;
import org.yjc.custom.UserUtils;
import org.yjc.entity.Audit;
import org.yjc.entity.Book;
import org.yjc.entity.SysUser;
import org.yjc.service.AuditService;
import org.yjc.service.BookService;
import org.yjc.service.SysUserService;

@Controller
@RequestMapping(value="/book")
public class AuditController {
	@Autowired
	private AuditService auditservice;
	@Autowired
	private BookService bookservice;
	@Autowired
	private UserUtils userutils;
	@Autowired 
	private SysUserService sysuserservice;
	@Autowired
	private JournalUtils journalutils;
	
	@RequestMapping(value="/audit")
	public String audit() {
		return "book/audit";
	}
	
	@RequestMapping(value="/auditbook")
	@ResponseBody
	public String check(Integer ids) {
		Book book = bookservice.findById(ids); //查询要借的书
		Integer numbers = book.getNumber(); //取出该书的库存量
		Integer number = numbers - 1; //书的库存减少1
		book.setNumber(number);
		bookservice.save(book); //存入新库存
		
		Integer userid = userutils.getUser().getId(); //获取到当前借书用户的id
		SysUser user = sysuserservice.findById(userid); //获取到当前借书人
		
		Audit audit = new Audit();
		audit.setBorrowername(user.getName()); //存入借书人姓名
		audit.setUsername(user.getUsername()); //存入借书人用户名
		audit.setBorrowerId(user.getId());	//存入借书人id
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		audit.setTime(df.format(new Date())); //存入借书时间(当前系统时间)
		audit.setBooknom(book.getBookname()); //存入图书名称
		audit.setBookid(book.getId()); //存入图书id
		audit.setBookwriter(book.getWriter()); //作者名称
		audit.setBookisbn(book.getIsbn()); //存入ibsn编号
		audit.setAccredit("等待图书管理员进行审核");
		audit.setBook(book);
		auditservice.save(audit);  //存入借书记录
		//录入日志
		journalutils.journal1("借书申请",user.getName()+"提交借"+"("+book.getBookname()+")的申请");
		return "OK";
	}
	
	@RequestMapping(value="/applyfor")
	@ResponseBody
	public Object list(DatagridUtils param,String borrowername,String booknom,String username) {
		Pageable pageable = param.buildPageable();
		Page<Audit> page = null;
		String term = "等待图书管理员进行审核"; //查询条件
		//安全查询
		Specification<Audit> spec = new Specification<Audit>() {
			@Override
			public Predicate toPredicate(Root<Audit> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if (StringUtils.hasText(borrowername)) {
					rules.add(criteriaBuilder.like(root.get("borrowername"), "%" + borrowername + "%"));
				} else if (StringUtils.hasText(username)) {
					rules.add(criteriaBuilder.like(root.get("username"), "%" + username + "%"));
				} else if (StringUtils.hasText(booknom)) {
					rules.add(criteriaBuilder.like(root.get("booknom"), "%" + booknom + "%"));
				}
					rules.add(criteriaBuilder.equal(root.get("accredit"),term));
					return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		page = auditservice.findAll(spec,pageable);
		HashMap<String, Object> result = DatagridUtils.biuldDatagridResult(page);
		return result;
	}
	
	@RequestMapping(value="/agree")
	@ResponseBody
	public String agree(Integer ids,String name) {
		SysUser sysuser = userutils.getUser();
		Audit audit  = auditservice.findById(ids);
		audit.setAccredit("已同意");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		audit.setAgreetime(df.format(new Date())); //存入同意时间
		auditservice.save(audit);
		//录入日志
		journalutils.journal1("同意借书申请",sysuser.getName()+"同意了"+"("+ name +")的借书申请");
		return "OK";
	}
	
	@RequestMapping(value="/agreemain")
	public String agree() {
		return "book/agreeall";
	}
	
	@RequestMapping(value="/agreelist")
	@ResponseBody
	public Object agreelist(DatagridUtils param,String borrowername,String booknom,String username) {
		Pageable pageable = param.buildPageable();
		Page<Audit> page = null;
		String term = "已同意"; //查询条件
		//安全查询
		Specification<Audit> spec = new Specification<Audit>() {
			@Override
			public Predicate toPredicate(Root<Audit> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if (StringUtils.hasText(borrowername)) {
					rules.add(criteriaBuilder.like(root.get("borrowername"), "%" + borrowername + "%"));
				} else if (StringUtils.hasText(username)) {
					rules.add(criteriaBuilder.like(root.get("username"), "%" + username + "%"));
				} else if (StringUtils.hasText(booknom)) {
					rules.add(criteriaBuilder.like(root.get("booknom"), "%" + booknom + "%"));
				}
					rules.add(criteriaBuilder.equal(root.get("accredit"),term));
					return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		page = auditservice.findAll(spec,pageable);
		HashMap<String, Object> result = DatagridUtils.biuldDatagridResult(page);
		return result;
	}
	
	@RequestMapping(value="nmtmain")
	public String nmtmain() {
		return "book/nmt";
		
	}
	
	@RequestMapping(value="/nmt")
	@ResponseBody
	public Object nmt(DatagridUtils param,String booknom,String bookwriter,String bookisbn){
		Integer userid = userutils.getUser().getId();
		Pageable pageable = param.buildPageable();
		Page<Audit> page = null;
		String term = "已同意"; //查询条件
		//安全查询
		Specification<Audit> spec = new Specification<Audit>() {
			@Override
			public Predicate toPredicate(Root<Audit> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if (StringUtils.hasText(bookwriter)) {
					rules.add(criteriaBuilder.like(root.get("bookwriter"), "%" + bookwriter + "%"));
				} else if (StringUtils.hasText(bookisbn)) {
					rules.add(criteriaBuilder.like(root.get("bookisbn"), "%" + bookisbn + "%"));
				} else if (StringUtils.hasText(booknom)) {
					rules.add(criteriaBuilder.like(root.get("booknom"), "%" + booknom + "%"));
				}
					rules.add(criteriaBuilder.equal(root.get("accredit"), term));
					rules.add(criteriaBuilder.equal(root.get("borrowerId"), userid));
					return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		page = auditservice.findAll(spec,pageable);
		HashMap<String, Object> result = DatagridUtils.biuldDatagridResult(page);
		return result;
	}
	
	@RequestMapping(value="nmtbook")
	@ResponseBody
	private String nmtbook(Integer ids,Integer bookid) {
		SysUser sysuser = userutils.getUser();
		Book book = bookservice.findById(bookid); //查询已借的书
		Integer numbers = book.getNumber(); //取出该书的库存量
		Integer number = numbers + 1; //书的库存加1
		book.setNumber(number);
		bookservice.save(book); //存入新库存
		
		Audit audit = auditservice.findByid(ids); //找到借书记录
		audit.setAccredit("已归还"); //存入新状态
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		audit.setReturntime(df.format(new Date())); //存入归还时间
		auditservice.save(audit);
		//录入日志
		journalutils.journal1("还书",sysuser.getName()+"已归还"+"("+book.getBookname()+")");
		return "OK";
	}
	
	@RequestMapping(value="returnbook")
	public String returnbook() {
		return "book/returnbook";
	}
	
	@RequestMapping(value="/returnlist")
	@ResponseBody
	public Object returnlist(DatagridUtils param,String booknom,String borrowername,String username){
		Pageable pageable = param.buildPageable();
		Page<Audit> page = null;
		String term = "已归还"; //查询条件
		//安全查询
		Specification<Audit> spec = new Specification<Audit>() {
			@Override
			public Predicate toPredicate(Root<Audit> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if (StringUtils.hasText(borrowername)) {
					rules.add(criteriaBuilder.like(root.get("borrowername"), "%" + borrowername + "%"));
				} else if (StringUtils.hasText(username)) {
					rules.add(criteriaBuilder.like(root.get("username"), "%" + username + "%"));
				} else if (StringUtils.hasText(booknom)) {
					rules.add(criteriaBuilder.like(root.get("booknom"), "%" + booknom + "%"));
				}
					rules.add(criteriaBuilder.equal(root.get("accredit"),term));
					return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		page = auditservice.findAll(spec,pageable);
		HashMap<String, Object> result = DatagridUtils.biuldDatagridResult(page);
		return result;
	}
	
	@RequestMapping(value="/delete1")
	@ResponseBody
	public String delete(Integer ids,String name,String bookname) {
		SysUser sysuser = userutils.getUser();
		auditservice.deleteById(ids);
		//录入日志
		journalutils.journal1("删除还书记录",sysuser.getName()+"删除了"+name+"还 ( "+bookname+")的记录");
		return "OK";
	}
	
	@RequestMapping(value="/waitmain")
	public String waitmain() {
		return "book/wait";
	}
	
	@RequestMapping(value="/waitlist")
	@ResponseBody
	public Object waitlist(DatagridUtils param,String booknom,String bookwriter,String bookisbn){
		Integer userid = userutils.getUser().getId();
		Pageable pageable = param.buildPageable();
		Page<Audit> page = null;
		String term = "等待图书管理员进行审核"; //查询条件
		//安全查询
		Specification<Audit> spec = new Specification<Audit>() {
			@Override
			public Predicate toPredicate(Root<Audit> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if (StringUtils.hasText(bookwriter)) {
					rules.add(criteriaBuilder.like(root.get("bookwriter"), "%" + bookwriter + "%"));
				} else if (StringUtils.hasText(bookisbn)) {
					rules.add(criteriaBuilder.like(root.get("bookisbn"), "%" + bookisbn + "%"));
				} else if (StringUtils.hasText(booknom)) {
					rules.add(criteriaBuilder.like(root.get("booknom"), "%" + booknom + "%"));
				}
					rules.add(criteriaBuilder.equal(root.get("accredit"), term));
					rules.add(criteriaBuilder.equal(root.get("borrowerId"), userid));
					return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		page = auditservice.findAll(spec,pageable);
		HashMap<String, Object> result = DatagridUtils.biuldDatagridResult(page);
		return result;
	}
	
	@RequestMapping(value="/cancel")
	@ResponseBody
	public String cancel(Integer ids,Integer bookid) {
		SysUser sysuser = userutils.getUser();
		Book book = bookservice.findById(bookid); //查找到这本书
		Integer number = book.getNumber(); //获取到库存量
		number = number + 1; //库存加1
		book.setNumber(number);
		bookservice.save(book);
		auditservice.deleteById(ids); //删除申请记录
		//录入日志
		journalutils.journal1("取消借书申请",sysuser.getName()+"取消了借("+book.getBookname()+")的申请 ");
		return "OK";
	}
}
